home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / ODUtils / Sources / ODMemory.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-16  |  11.5 KB  |  391 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODMemory.cpp
  3.  
  4.     Contains:    Procedural implementation to the Memory component
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright: © 1993-95 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11. #ifndef _ODMEMORY_
  12. #include "ODMemory.h"
  13. #endif
  14.  
  15. #ifndef _MEMMGR_
  16. #include "MemMgr.h"
  17. #endif
  18.  
  19. #ifndef __CODEFRAGMENTS__
  20. #include <CodeFragments.h>
  21. #endif
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef _EXCEPT_
  28. #include "Except.h"
  29. #endif
  30.  
  31. #ifndef _ODDEBUG_
  32. #include "ODDebug.h"
  33. #endif
  34.  
  35. #ifndef _UTILERRS_
  36. #include "UtilErrs.h"
  37. #endif
  38.  
  39. //========================================================================================
  40. // Constants
  41. //========================================================================================
  42.  
  43. const size_t kSystemHeapInitialSize = 20 * 1024;
  44. const size_t kSystemHeapGrowBySize  = 32 * 1024;
  45.  
  46. const ODSize kMinAppHeapSpace        = 16 * 1024;
  47. const ODSize kMinContigAppHeapSpace =  6 * 1024;
  48.  
  49. //========================================================================================
  50. // Global variables
  51. //========================================================================================
  52.  
  53. //static ODMemoryHeapID    gDefaultHeap; // ----- [HLX] Changed  -----
  54. #if 0
  55. #pragma lib_export on                // Need to tell compiler that gSystemHeap is imported
  56. extern void* gSystemHeap;            // Lives in Shared Globals library, MemShard.cpp.
  57. #pragma lib_export off
  58. #endif /* 0 */
  59. //========================================================================================
  60. // Initialization
  61. //========================================================================================
  62.  
  63.  
  64. OSErr InitODMemory( )
  65. {
  66. // ----- [HLX] Changed Begin -----
  67. //    gDefaultHeap = MMGetDefaultHeap();
  68. //    return gDefaultHeap ?noErr :memFullErr;
  69.     return noErr;
  70. // ----- [HLX] Changed End -----
  71. }
  72.  
  73. // Note: CFMInit stuff is now in UtilInit.cpp
  74.  
  75. //extern "C" pascal OSErr UtilitiesCFMInit( CFragInitBlockPtr );
  76.  
  77. // For use inside OpenDoc only. Parts won't need to use this but
  78. // should call InitODMemory from their own CFMInit routines.
  79. //pascal OSErr UtilitiesCFMInit (CFragInitBlockPtr /*initBlkPtr*/)
  80. //{
  81. //    return InitODMemory();
  82. //}
  83.  
  84.  
  85. //========================================================================================
  86. // Function declarations for operations on pointer based blocks
  87. //========================================================================================
  88.  
  89.  
  90. //----------------------------------------------------------------------------------------
  91. // ODGetDefaultHeap
  92. //----------------------------------------------------------------------------------------
  93.  
  94. ODMemoryHeapID ODGetDefaultHeap()
  95. {
  96. // ----- [HLX] Changed Begin -----
  97.     // Tweaked by JEL on 10/31/95
  98.     // Don't use FW_CMemoryTaskGlobals: 
  99.     // 1) It's a circular dependency
  100.     // 2) We may remove soon remove FW_CMemoryTaskGlobals entirely
  101.     // We may want to restore the global gDefaultHeap and use it here.
  102.     return ::MMGetDefaultHeap();
  103. //    ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  104. //    return gDefaultHeap;
  105. // ----- [HLX] Changed End -----
  106. }
  107. #if 0
  108. //----------------------------------------------------------------------------------------
  109. // ODGetSystemHeap
  110. //----------------------------------------------------------------------------------------
  111.  
  112. ODMemoryHeapID ODGetSystemHeap()
  113. {
  114.     // The system heap should be created only by the OD system process.
  115.     
  116.     if( !gSystemHeap ) {
  117.         gSystemHeap = ODCreateHeap(    kSystemHeapInitialSize,
  118.                                     kSystemHeapGrowBySize,
  119.                                     kODTrue,"OpenDoc System Heap" );
  120.         THROW_IF_NULL(gSystemHeap);
  121.     }
  122.     return (ODMemoryHeapID)gSystemHeap;
  123. }
  124. #endif /* 0 */
  125. //----------------------------------------------------------------------------------------
  126. // ODCreateHeap
  127. //----------------------------------------------------------------------------------------
  128.  
  129. ODMemoryHeapID ODCreateHeap(unsigned long sizeInitial, unsigned long sizeIncrement,
  130.                             Boolean fromSysMemory, const char *name)
  131. {
  132.     MemHeap *heap = MMNewHeap(fromSysMemory ?kMMSysMemory :kMMTempMemory,
  133.                               sizeInitial, sizeIncrement, name);
  134.     if( !heap )
  135.         THROW(kODErrOutOfMemory);
  136.     return heap;
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // ODDestroyHeap
  141. //----------------------------------------------------------------------------------------
  142.  
  143. void ODDestroyHeap(ODMemoryHeapID heapID)
  144. {
  145.     MMDisposeHeap(heapID);
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. // ODNewPtr
  150. //----------------------------------------------------------------------------------------
  151.  
  152. void *ODNewPtr(ODBlockSize blkSize, ODMemoryHeapID heapID)
  153. {
  154.     if( heapID == kODNULL ) {
  155. // ----- [HLX] Changed Begin -----
  156. //        ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  157. //        heapID = gDefaultHeap;
  158.         heapID = ODGetDefaultHeap();
  159. // ----- [HLX] Changed End -----
  160.     }
  161.     void *block = MMAllocateIn(blkSize,heapID);
  162.     if( !block )
  163.         THROW(kODErrOutOfMemory);
  164.     return block;
  165. }
  166.  
  167. //----------------------------------------------------------------------------------------
  168. // ODNewPtrClear
  169. //----------------------------------------------------------------------------------------
  170.  
  171. void *ODNewPtrClear(ODBlockSize blkSize, ODMemoryHeapID heapID)
  172. {
  173.     if( heapID == kODNULL ) {
  174. // ----- [HLX] Changed Begin -----
  175. //        ASSERTM(gDefaultHeap,kODErrAssertionFailed,"You didn't call InitODMemory!");
  176. //        heapID = gDefaultHeap;
  177.         heapID = ODGetDefaultHeap();
  178. // ----- [HLX] Changed End -----
  179.     }
  180.     void *block = MMAllocateClearIn(blkSize,heapID);
  181.     if( !block )
  182.         THROW(kODErrOutOfMemory);
  183.     return block;
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. // ODReallocate
  188. //----------------------------------------------------------------------------------------
  189.  
  190. void *ODReallocate(void *block, ODBlockSize newSize)
  191. {
  192.     block = MMReallocate(block,newSize);
  193.     if( !block && newSize>0 )
  194.         THROW(kODErrOutOfMemory);
  195.     return block;
  196. }
  197.  
  198. //------------------------------------------------------------------------------
  199. // ODDisposePtr
  200. //------------------------------------------------------------------------------
  201.  
  202. void ODDisposePtr(void *pointer)
  203. {
  204.     MMFree(pointer);
  205. }
  206.  
  207. //----------------------------------------------------------------------------------------
  208. // ODRecoverHeapID
  209. //----------------------------------------------------------------------------------------
  210.  
  211. ODMemoryHeapID ODRecoverHeapID(const void *block)
  212. {
  213.     ODMemoryHeapID heap = MMGetHeap(block);
  214.     if( !heap )
  215.         THROW(kODErrInvalidBlock);
  216.     return heap;
  217. }
  218.  
  219.  
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // ODBlockIsObject
  223. //----------------------------------------------------------------------------------------
  224.  
  225. void ODBlockIsObject( void *block, ODBoolean isObject )
  226. {
  227.     MMSetIsObject(block,isObject);
  228. }
  229.  
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // ODIsBlockAnObject
  233. //----------------------------------------------------------------------------------------
  234.  
  235. ODBoolean ODIsBlockAnObject( const void *block )
  236. {
  237.     return MMIsObject(block);
  238. }
  239.  
  240.  
  241. //========================================================================================
  242. // Function declarations for operations on handle based blocks
  243. //========================================================================================
  244.  
  245. //----------------------------------------------------------------------------------------
  246. // ODNewHandle
  247. //----------------------------------------------------------------------------------------
  248.  
  249. ODHandle ODNewHandle(ODULong howBig)
  250. {
  251.     ODHandle h = (ODHandle) MMAllocateHandleIn(howBig,kMMTempMemory);
  252.     if( !h )
  253.         THROW(kODErrOutOfMemory);
  254.     return h;
  255.         
  256. }
  257.  
  258. //----------------------------------------------------------------------------------------
  259. // ODDisposeHandle
  260. //----------------------------------------------------------------------------------------
  261.  
  262. void ODDisposeHandle(ODHandle handle)
  263. {
  264.     MMFreeHandle((MMHandle)handle);
  265. }
  266.  
  267. //----------------------------------------------------------------------------------------
  268. // ODCopyHandle
  269. //----------------------------------------------------------------------------------------
  270.  
  271. ODHandle ODCopyHandle(ODHandle handle)
  272. {
  273.     ODHandle h = (ODHandle) MMCopyHandle((MMHandle)handle);
  274.     if( !h )
  275.         THROW(kODErrOutOfMemory);
  276.     return h;
  277. }
  278.  
  279. //----------------------------------------------------------------------------------------
  280. // ODGetHandleSize(ODHandle handle)
  281. //----------------------------------------------------------------------------------------
  282.  
  283. ODULong ODGetHandleSize(ODHandle handle)
  284. {
  285.     return MMGetHandleSize((MMHandle)handle);
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. // ODSetHandleSize(ODHandle handle, ODULong blkSize)
  290. //----------------------------------------------------------------------------------------
  291.  
  292. void ODSetHandleSize(ODHandle handle, ODULong blkSize)
  293. {
  294.     if( !MMSetHandleSize((MMHandle)handle,blkSize) )
  295.         THROW(kODErrOutOfMemory);
  296.  
  297. }
  298.  
  299. //----------------------------------------------------------------------------------------
  300. // ODLockHandle(ODHandle handle)
  301. //----------------------------------------------------------------------------------------
  302.  
  303. void* ODLockHandle(ODHandle handle)
  304. {
  305.     void *p = MMLockHandle((MMHandle)handle);
  306.     if( !p )
  307.         THROW(kODErrOutOfMemory);
  308.     return p;
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. // ODUnlockPtr(void* ptr)
  313. //----------------------------------------------------------------------------------------
  314.  
  315. void ODUnlockPtr(void* ptr)
  316. {
  317.     MMUnlockPtr(ptr);
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. // ODUnlockHandle(ODHandle handle)
  322. //----------------------------------------------------------------------------------------
  323.  
  324. void ODUnlockHandle(ODHandle handle)
  325. {
  326.     MMUnlockHandle((MMHandle)handle);
  327. }
  328.  
  329.  
  330. //========================================================================================
  331. // Function declarations utility functions
  332. //========================================================================================
  333.  
  334. //------------------------------------------------------------------------------
  335. // ODBlockMove
  336. //------------------------------------------------------------------------------
  337.  
  338.  
  339. void ODBlockMove( const void *from, void *to, ODULong size)
  340. {
  341.     MMMove(to,from,size);
  342. }
  343.  
  344.  
  345. //------------------------------------------------------------------------------
  346. // ODNewRgn
  347. //------------------------------------------------------------------------------
  348.  
  349. RgnHandle ODNewRgn( )
  350. {
  351.     RgnHandle r = (RgnHandle) ODNewHandle(sizeof(Region));
  352.     (**r).rgnSize = sizeof(Region);
  353.     SetRect(&(**r).rgnBBox, 0,0,0,0);
  354.     return r;
  355. }
  356.  
  357. //------------------------------------------------------------------------------
  358. // ODRequireFreeSpace
  359. //------------------------------------------------------------------------------
  360.  
  361.  
  362. ODBoolean
  363. ODHaveFreeSpace( ODSize haveTotal, ODSize haveContig /*=0*/,
  364.                  ODBoolean appHeap /*=false*/ )
  365. {
  366.     size_t total, contig;
  367.     if( appHeap )
  368.         MMSystemFreeSpace(kMMAppMemory, &total,&contig);
  369.     else
  370.         MMGetFreeSpace(ODGetDefaultHeap(),&total,&contig ); // [HLX] changed
  371.     return total>=haveTotal && contig>=haveContig;
  372. }
  373.  
  374. void
  375. ODRequireFreeSpace( ODSize total, ODSize contig, ODBoolean appHeap )
  376. {
  377.     if( !ODHaveFreeSpace(total,contig,appHeap) )
  378.         THROW(appHeap ?memFullErr :kODErrOutOfMemory);
  379.     
  380.     // I decided it made sense to throw a Mac mem mgr error if the
  381.     // app heap is full, since this is the error you'd get if you
  382.     // tried to d
  383. }
  384.  
  385. void
  386. ODCheckAppHeapSpace( )
  387. {
  388.     if( !ODHaveFreeSpace(kMinAppHeapSpace,kMinContigAppHeapSpace,kODTrue) )
  389.         THROW(memFullErr);
  390. }
  391.